import heapq

class Solution:
       def eatenApples(self, apples: list[int], days: list[int]) -> int:
            n, ans, i = len(apples), 0, 0
            h = []
            while i < n or h:
                if i < n and apples[i]:
                    heapq.heappush(h, [i+days[i], apples[i]])
                while h and (i >= h[0][0] or h[0][1] == 0):
                    heapq.heappop(h)
                if h:
                    h[0][1] -= 1
                    ans += 1
                i += 1
            return ans

if __name__ == "__main__":
    s = Solution()
    print(s.eatenApples(apples = [1,2,3,5,2], days = [3,2,1,4,2]))
    print(s.eatenApples(apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]))